class LRUCache {
    constructor(capacity) {
        this.capacity = capacity;
        this.cache = new Map();
    }

    get(key) {
        if (!this.cache.has(key)) return -1;
        
        // 提取并重新插入以更新顺序
        const value = this.cache.get(key);
        this.cache.delete(key);
        this.cache.set(key, value);
        return value;
    }

    put(key, value) {
        // 如果存在则删除以更新顺序
        if (this.cache.has(key)) {
            this.cache.delete(key);
        }
        
        // 插入新值
        this.cache.set(key, value);
        
        // 超出容量则删除最旧项
        if (this.cache.size > this.capacity) {
            this.cache.delete(this.cache.keys().next().value);
        }
    }
}
